#!/bin/bash
#$Id: _base_script_block.wrap,v 1.1 2012-05-07 13:47:27 remik Exp $

# Load usefull functions
if [ ! -f $HOME/scripto/bash/bash_library.sh ]; then
  echo "[error] $HOME/scripto/bash/bash_library.sh not found. Exiting. "
  exit 1
else
  . $HOME/scripto/bash/bash_library.sh
fi

#INFO_MODE=DEBUG
INFO_MODE=INFO

msgi "Editions"
f_R12_ADZDSHOWED

echo ""
msgi "ADOP status"
f_R12_ADOP_STATUS

f_execute_sql "create user BOLINF identified by xxx;"
cat $F_EXECUTE_SQL

f_execute_sql "@valid;"
cat $F_EXECUTE_SQL

b_grant_normal_packages()
{
  msgb "${FUNCNAME[0]} Beginning."

  while read LINE
  do
    f_user_execute_sql "select count(1) from dba_objects where status != 'VALID';" "apps/apps"
    V_INVALID_BEFORE=$V_EXECUTE_SQL
  
    f_user_execute_sql "select edition_name from dba_objects where object_name='$LINE';" "apps/apps"
    V_EDITION=$V_EXECUTE_SQL
  
    f_user_execute_sql "grant execute on $LINE to BOLINF;" "apps/apps"
    V_GRANT_RESULT=$V_EXECUTE_SQL

    f_user_execute_sql "select count(1) from dba_objects where status != 'VALID';" "apps/apps"
    V_INVALID_AFTER=$V_EXECUTE_SQL

    f_user_execute_sql "select edition_name from dba_objects where object_name='$LINE';" "apps/apps"
    V_EDITION_AFTER=$V_EXECUTE_SQL

    echo "$LINE $V_INVALID_BEFORE $V_EDITION $V_GRANT_RESULT $V_INVALID_AFTER $V_EDITION_AFTER" 
    if [ "$V_INVALID_AFTER" -gt "$V_INVALID_BEFORE" ]; then
      echo "New invalids"
    fi

#exit 0
  done < packages.lst

  msgb "${FUNCNAME[0]} Finished."
} #b_grant_normal_packages


b_grant_normal_tables()
{
  msgb "${FUNCNAME[0]} Beginning."
  while read LINE
  do
    f_user_execute_sql "select count(1) from dba_objects where status != 'VALID';" "apps/apps"
    V_INVALID_BEFORE=$V_EXECUTE_SQL
  
    f_user_execute_sql "select edition_name from dba_objects where object_name='$LINE';" "apps/apps"
    V_EDITION=$V_EXECUTE_SQL
  
    f_user_execute_sql "grant select on $LINE to BOLINF;" "apps/apps"
    V_GRANT_RESULT=$V_EXECUTE_SQL

    f_user_execute_sql "select count(1) from dba_objects where status != 'VALID';" "apps/apps"
    V_INVALID_AFTER=$V_EXECUTE_SQL

    f_user_execute_sql "select edition_name from dba_objects where object_name='$LINE';" "apps/apps"
    V_EDITION_AFTER=$V_EXECUTE_SQL

    echo "$LINE $V_INVALID_BEFORE $V_EDITION $V_GRANT_RESULT $V_INVALID_AFTER $V_EDITION_AFTER" 
    if [ "$V_INVALID_AFTER" -gt "$V_INVALID_BEFORE" ]; then
      echo "New invalids"
    fi

#exit 0
  done < tables.lst

  msgb "${FUNCNAME[0]} Finished."
} #b_grant_normal_tables

# Execution of blocks
# If parameter 'block_name' was provided I execute only specified block
SELECT_BLOCK=$1

msgi "Selectted block: $SELECT_BLOCK"

case $SELECT_BLOCK in
  "b_grant_normal_tables")
    b_grant_normal_tables
    ;;
  "b_grant_normal_packages")
    b_grant_normal_packages
    ;;
  "b_grant_normal")
    b_grant_normal_tables
    b_grant_normal_packages
    ;;
  *)
    echo "Please provide the block that You want to run."
    echo "Available blocks: "
    echo " b_grant_normal_tables"
    echo " b_grant_normal_packages"
    echo ""
    echo "Available sets of blocks:"
    echo " b_grant_normal"

    exit 1
    ;;
esac

msgi "Done."



f_execute_sql "@valid;"
cat $F_EXECUTE_SQL
